package com.timmy.google_play.holder.detail;

import android.animation.ValueAnimator;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.conf.Constants;
import com.timmy.google_play.utils.UIUtils;

import java.util.List;

import butterknife.BindView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Timmy on 2016/12/14.
 */
public class DetailSafeHolder extends BaseHolder<ItemBean> implements View.OnClickListener {
    @BindView(R.id.app_detail_safe_iv_arrow)
    ImageView mAppDetailSafeIvArrow;

    @BindView(R.id.app_detail_safe_pic_container)
    LinearLayout mAppDetailSafePicContainer;

    @BindView(R.id.app_detail_safe_des_container)
    LinearLayout mAppDetailSafeDesContainer;

    private boolean isOpen = true;

    @Override
    protected View initHolderVIew() {

        View view = View.inflate(UIUtils.getContext(), R.layout.item_detail_safe, null);

        view.setOnClickListener(this);
        return view;
    }

    @Override
    protected void refreshHolderView(ItemBean data) {

        //获取安全信息集合
        List<ItemBean.SafeBean> safeList = data.safe;

        for (ItemBean.SafeBean safeBean : safeList) {

            //设置上面的图标
            String safeUrl = safeBean.safeUrl;

            String safeDes      = safeBean.safeDes;
            int    safeDesColor = safeBean.safeDesColor;
            String safeDesUrl   = safeBean.safeDesUrl;

            //填充 mAppDetailSafePicContainer 容器中的具体内容,图片
            ImageView ivIcon = new ImageView(UIUtils.getContext());
            //加载图片
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMAGEBASEURL + safeUrl).into(ivIcon);
            mAppDetailSafePicContainer.addView(ivIcon);

            //填充 mAppDetailSafeDesContainer 容器中的具体内容
            LinearLayout linearLayout = new LinearLayout(UIUtils.getContext());

            int padding = UIUtils.dp2px(3);
            linearLayout.setPadding(UIUtils.dp2px(10), padding, padding, padding);
            linearLayout.setGravity(Gravity.CENTER_VERTICAL);

            // 孩子
            ImageView ivDesIcon = new ImageView(UIUtils.getContext());
            TextView  tvDesNote = new TextView(UIUtils.getContext());

            // 赋值操作
            tvDesNote.setText(safeDes);
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMAGEBASEURL + safeDesUrl).into(ivDesIcon);

            //修改颜色
            if (safeDesColor == 0) {
                tvDesNote.setTextColor(UIUtils.getColor(R.color.app_detail_safe_normal));
            } else {
                tvDesNote.setTextColor(UIUtils.getColor(R.color.app_detail_safe_warning));
            }

            //加入到每一行
            linearLayout.addView(ivDesIcon);
            linearLayout.addView(tvDesNote);

            mAppDetailSafeDesContainer.addView(linearLayout);
        }

    }

    @Override
    public void onClick(View view) {
        changeAppDetailSafeDesContainerHeight(true);
    }

    private void changeAppDetailSafeDesContainerHeight(boolean isAnimation) {

        if (isOpen) {

            // 折叠 mAppDetailSafeDesContainer
            int start = mAppDetailSafeDesContainer.getMeasuredHeight();
            int end   = 0;

            if (isAnimation) {
                doAnimation(start, end);
            } else {
                //不需要动画,直接修改高度

                ViewGroup.LayoutParams params = mAppDetailSafeDesContainer.getLayoutParams();

                params.height = end;
                mAppDetailSafeDesContainer.setLayoutParams(params);
            }

        } else {    //展开
            int start = 0;

            mAppDetailSafeDesContainer.measure(0, 0);

            int end = mAppDetailSafeDesContainer.getMeasuredHeight();       //触发重新测量

            if (isAnimation) {
                doAnimation(start, end);
            }else{
                //直接修改高度
                ViewGroup.LayoutParams params = mAppDetailSafeDesContainer.getLayoutParams();

                params.height = end;
                mAppDetailSafeDesContainer.setLayoutParams(params);
            }
        }

        //取反
        isOpen = !isOpen;
    }

    private void doAnimation(int start, int end) {

        // 使用属性动画
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.start();

        //监听得到动画执行过程中的渐变值
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int height = (int) valueAnimator.getAnimatedValue();

                //修改容器的实际高度
                ViewGroup.LayoutParams params = mAppDetailSafeDesContainer.getLayoutParams();

                params.height = height;
                mAppDetailSafeDesContainer.setLayoutParams(params);
            }
        });
    }
}
